@extends('admin.common.main')
@section('cnt')
    <div class="layuimini-container">
        <div class="layuimini-main">
            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">预约名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="name" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    {!! authBtn('admin.notice.create','add','添加') !!}
{{--                    {!! authBtn('admin.notice.export','export','导出') !!}--}}
                </div>
            </script>
            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
            <script type="text/html" id="currentTableBar">
                {!! authBtn('admin.notice.edit','edit') !!}
                {!! authBtn('admin.notice.destroy','delete') !!}
            </script>
        </div>
    </div>
@endsection
@section('js')
    <script>
        layui.use(['form', 'table','http'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                http  = layui.http,
                apiurl =http.api;
            init();
            const  csrf = "{{csrf_token()}}";
            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                var result = JSON.stringify(data.field);
                init(data.field);
                return false;
            });
            function init(resulti){
                http.render(
                    '{{route('admin.notice.index')}}',
                    [[
                        // {type: "checkbox", width: 50},
                        {field: 'id', width: 150, title: 'ID', sort: true},
                        {field: 'fd_id', width: 150, title: '房东'},
                        {field: 'us_id', width: 150, title: '租客'},
                        {field: 'cnt', width: 250, title: '内容'},
                        {field: 'status', width: 250, title: '状态'},
                        {field: 'created_at', width: 200, title: '创建时间', sort: true},
                        {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
                    ]],
                    resulti
                );
            }
            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '文章添加',
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '{{route('admin.notice.create')}}',
                        success: function(layero, index){
                            console.log(layero, index);
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                }
            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                console.log(obj)
            });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                var id = obj.data.id;
                console.error(data,del);
                if (obj.event === 'edit') {
                    var url = "{{route('admin.notice.edit',['id'=>1])}}";
                    console.error("url",url);
                    var url =  url.replace('1', id);
                    var index = layer.open({
                        title: '编辑',
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: url,
                    });
                    return false;
                }
                else if (obj.event === 'delete') {
                    var del = "{{route('admin.notice.destroy',['id'=>1])}}";
                    layer.confirm('真的删除么', function (index) {
                        var url =  del.replace('1', id);
                        http.httpFetch( url,'',csrf,"DELETE",function(res) {
                            console.error(res);
                            if(res.code === 200){
                                layer.msg(res.msg, {time: 1800, icon: 1});
                                obj.del();
                                init();
                            }else{
                                layer.msg(res.msg, {time: 1800, icon: 2});
                            }
                        });
                    });
                }
            });
        });
    </script>
@endsection

